package com.ynkj.teleprompter.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ynkj.teleprompter.BaseActivity;
import com.ynkj.teleprompter.R;
import com.ynkj.teleprompter.adapter.AddtcAdapter;
import com.ynkj.teleprompter.db.TeleprompterService;
import com.ynkj.teleprompter.utiles.Utilities;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rcy_tc)
    RecyclerView rcyTc;
    @BindView(R.id.btn_adddata)
    Button btnAdddata;
    @BindView(R.id.tv_etem)
    TextView tvEtem;

    private LinearLayoutManager manager;
    private AddtcAdapter adapter;
    ArrayList<String> list=new ArrayList<>();
    public static String content="";

    @Override
    public int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        initView();
        initRcy();
    }

    private void initView() {
        teleprompterService=new TeleprompterService(mContext);
        list=teleprompterService.dbQueryAll();
        if(list.size()>0){
            rcyTc.setVisibility(View.VISIBLE);
            tvEtem.setVisibility(View.GONE);
        }else {
            rcyTc.setVisibility(View.GONE);
            tvEtem.setVisibility(View.VISIBLE);
        }
    }

    private void initRcy() {
        manager=new LinearLayoutManager(this);
        adapter=new AddtcAdapter(R.layout.item_tc,list);
        rcyTc.setLayoutManager(manager);
        rcyTc.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        rcyTc.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                // TODO Auto-generated method stub
                Intent serviceIntent = new Intent(MainActivity.this,
                        TcActivity.class);
                content=adapter.getItem(position).toString();
                startActivity(serviceIntent);

            }
        });
    }


    @OnClick({R.id.btn_adddata})
    public void onBindClick(View view) {
        switch (view.getId()) {
            case R.id.btn_adddata:
                Diaolog();
                break;
        }
    }

    private TeleprompterService teleprompterService;

    public void Diaolog() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_tc, null);
        final Dialog dialog = new Dialog(mContext, R.style.MyDialogPay);
        dialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        Window window = dialog.getWindow();
        WindowManager.LayoutParams wl = window.getAttributes();
        // 设置对齐的方法
        wl.gravity = Gravity.CENTER;
        // 以下这两句是为了保证按钮可以水平满屏
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
        // 设置显示位置
        dialog.onWindowAttributesChanged(wl);
        // 设置点击外围解散
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        EditText etContent=view.findViewById(R.id.et_content);
        TextView tvDeleteOk=view.findViewById(R.id.tv_delete_ok);
        TextView tvDeleteCacel=view.findViewById(R.id.tv_delete_cacel);
        tvDeleteCacel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        tvDeleteOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                boolean isSuccess=teleprompterService.InsertAddress(etContent.getText().toString());
                if(isSuccess){
                    Utilities.TOAST(mContext,"添加成功");
                    ArrayList<String> list=teleprompterService.dbQueryAll();
                    if(list.size()>0){
                        rcyTc.setVisibility(View.VISIBLE);
                        tvEtem.setVisibility(View.GONE);
                    }else {
                        rcyTc.setVisibility(View.GONE);
                        tvEtem.setVisibility(View.VISIBLE);
                    }
                    Log.e("mtest","------------添加了数据"+list.toString());
                    adapter.setNewData(list);
                    adapter.notifyDataSetChanged();
                }
                dialog.dismiss();
            }
        });
    }
}
